﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DataAccess
{
    /// <summary>
    /// 数据读取
    /// </summary>
    public class DataReader
    {
        /// <summary>
        /// 获取电影
        /// </summary>
        public List<MOVIE> GetMovies(string name, string type, string region, string year, int start, int length)
        {
            BlueGodEntities w = new BlueGodEntities();
            IQueryable<MOVIE> result = (from m in w.MOVIE
                                        where ((null == name || string.Empty == name || (m.name.IndexOf(name) >= 0))
                                            && (null == type || string.Empty == type || (m.type.IndexOf(type) >= 0))
                                            && (null == region || string.Empty == region || (m.region.IndexOf(region) >= 0))
                                            && (null == year || string.Empty == year || (m.first_showtime.IndexOf(year) >= 0)))
                                        orderby m.create_time descending
                                        select m).Skip(start).Take(length); ;

            List<MOVIE> records = new List<MOVIE>();
            try
            {
                foreach (MOVIE iter in result)
                {
                    records.Add(iter);
                }
            }
            catch (Exception e)
            {
                Public.WriteLog("GetMovies Exception: " + e.Message);
                throw;
            }
            return records;
        }

        /// <summary>
        /// 获取电视剧
        /// </summary>
        public List<TV> GetTVs(string name, string type, string region, string year, int start, int length)
        {
            BlueGodEntities w = new BlueGodEntities();
            IQueryable<TV> result = (from m in w.TV
                                     where ((null == name || string.Empty == name || (m.name.IndexOf(name) >= 0))
                                         && (null == type || string.Empty == type || (m.type.IndexOf(type) >= 0))
                                         && (null == region || string.Empty == region || (m.region.IndexOf(region) >= 0))
                                         && (null == year || string.Empty == year || (m.first_showtime.IndexOf(year) >= 0)))
                                     orderby m.create_time descending
                                     select m).Skip(start).Take(length); ;

            List<TV> records = new List<TV>();
            try
            {
                foreach (TV iter in result)
                {
                    records.Add(iter);
                }
            }
            catch (Exception e)
            {
                Public.WriteLog("GetTVs Exception: " + e.Message);
                throw;
            }
            return records;
        }

        /// <summary>
        /// 获取动漫
        /// </summary>
        public List<CARTOON> GetCartoons(string name, string type, string region, string year, int start, int length)
        {
            BlueGodEntities w = new BlueGodEntities();
            IQueryable<CARTOON> result = (from m in w.CARTOON
                                          where ((null == name || string.Empty == name || (m.name.IndexOf(name) >= 0))
                                              && (null == type || string.Empty == type || (m.type.IndexOf(type) >= 0))
                                              && (null == region || string.Empty == region || (m.region.IndexOf(region) >= 0))
                                              && (null == year || string.Empty == year || (m.first_showtime.IndexOf(year) >= 0)))
                                          orderby m.create_time descending
                                          select m).Skip(start).Take(length); ;

            List<CARTOON> records = new List<CARTOON>();
            try
            {
                foreach (CARTOON iter in result)
                {
                    records.Add(iter);
                }
            }
            catch (Exception e)
            {
                Public.WriteLog("GetCartoons Exception: " + e.Message);
                throw;
            }
            return records;
        }

        /// <summary>
        /// 获取综艺
        /// </summary>
        public List<SHOW> GetShows(string name, string type, string region, string year, int start, int length)
        {
            BlueGodEntities w = new BlueGodEntities();
            IQueryable<SHOW> result = (from m in w.SHOW
                                       where ((null == name || string.Empty == name || (m.name.IndexOf(name) >= 0))
                                           && (null == type || string.Empty == type || (m.type.IndexOf(type) >= 0))
                                           && (null == region || string.Empty == region || (m.region.IndexOf(region) >= 0))
                                           && (null == year || string.Empty == year || (m.first_showtime.IndexOf(year) >= 0)))
                                       orderby m.create_time descending
                                       select m).Skip(start).Take(length); ;

            List<SHOW> records = new List<SHOW>();
            try
            {
                foreach (SHOW iter in result)
                {
                    records.Add(iter);
                }
            }
            catch (Exception e)
            {
                Public.WriteLog("GetShows Exception: " + e.Message);
                throw;
            }
            return records;
        }
    }
}
